package com.sljy.hzk.view.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import com.sljy.hzk.R;
import com.sljy.hzk.entity.ExamMainEntity;
import com.sljy.hzk.entity.TaskDetailEntity;
import com.sljy.hzk.util.TimeUtils;
import com.sljy.hzk.view.ChoiceLayout;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created  on 2018/7/6 0006
 *
 * @author 唐开阔
 * @describe 我的任务-题目adapter
 */
public class TaskDoSubjectAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private final static int TYPE1 = 1;//文本
    private final static int TYPE2 = 2;//单选
    private final static int TYPE3 = 3;//多选
    private final static int TYPE4 = 4;//输入框
    private final static int TYPE5 = 5;//文本域
    private List<TaskDetailEntity.Subitem> subitemList;
    private Context mContext;

    public TaskDoSubjectAdapter(List<TaskDetailEntity.Subitem> subitemList, Context mContext) {
        this.subitemList = subitemList;
        this.mContext = mContext;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view;
        switch (viewType) {
            case TYPE1:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_task_type_1, parent, false);
                return new TaskDoSubjectAdapter.ViewHolder1(view);
            case TYPE2:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_task_type_2, parent, false);
                return new TaskDoSubjectAdapter.ViewHolder2(view, true);
            case TYPE3:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_task_type_2, parent, false);
                return new TaskDoSubjectAdapter.ViewHolder2(view, false);
            case TYPE4:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_task_type_4, parent, false);
                return new TaskDoSubjectAdapter.ViewHolder4(view);
            case TYPE5:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_task_type_5, parent, false);
                return new TaskDoSubjectAdapter.ViewHolder5(view);


        }
        view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_task_type_2, parent, false);
        return new TaskDoSubjectAdapter.ViewHolder2(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        switch (holder.getItemViewType()) {
            case TYPE1:
                ViewHolder1 viewHolder1 = (ViewHolder1) holder;
                viewHolder1.bindData(subitemList.get(position).getDefaultValue());
                break;
            case TYPE2:
                ViewHolder2 viewHolder2 = (ViewHolder2) holder;
                viewHolder2.bindData(subitemList.get(position));
                break;
            case TYPE3:
                ViewHolder2 viewHolder32 = (ViewHolder2) holder;
                viewHolder32.bindData(subitemList.get(position));
                break;
            case TYPE4:
                ViewHolder4 viewHolder4 = (ViewHolder4) holder;
                viewHolder4.bindData(subitemList.get(position));
                break;
            case TYPE5:
                ViewHolder5 viewHolder5 = (ViewHolder5) holder;
                viewHolder5.bindData(subitemList.get(position));
                break;

        }
    }

    @Override
    public int getItemViewType(int position) {
        return Integer.valueOf(subitemList.get(position).getType());
    }

    @Override
    public int getItemCount() {
        return subitemList.size();
    }

    public class ViewHolder1 extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_name)
        TextView textView;

        public ViewHolder1(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        public void bindData(String s) {
            textView.setText(s);
        }

    }

    public class ViewHolder2 extends RecyclerView.ViewHolder {
        @BindView(R.id.chose_layout)
        ChoiceLayout choiceLayout;
        @BindView(R.id.tv_must)
        TextView textView;

        public ViewHolder2(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        public ViewHolder2(View itemView, boolean isSinggle) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            choiceLayout.setSingleCheck(isSinggle);
        }

        public void bindData(TaskDetailEntity.Subitem item) {
            choiceLayout.bindOption(item.getItems());
            textView.setVisibility(item.getRequired().equals("1") ? View.VISIBLE : View.GONE);

        }
    }

    public class ViewHolder3 extends RecyclerView.ViewHolder {


        public ViewHolder3(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }


    }

    public class ViewHolder4 extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_must)
        TextView textView;
        @BindView(R.id.edit)
        EditText editText;

        public ViewHolder4(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        public void bindData(TaskDetailEntity.Subitem item) {
            editText.setHint(item.getDefaultValue());
            editText.setText(item.getEditStr());
            textView.setVisibility(item.getRequired().equals("1") ? View.VISIBLE : View.GONE);
            editText.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    item.setEditStr(s.toString());
                }

                @Override
                public void afterTextChanged(Editable s) {

                }
            });
        }

    }

    public class ViewHolder5 extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_must)
        TextView textView;
        @BindView(R.id.edit)
        EditText editText;


        public ViewHolder5(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        public void bindData(TaskDetailEntity.Subitem item) {
            editText.setHint(item.getDefaultValue());
            editText.setText(item.getEditStr());
            textView.setVisibility(item.getRequired().equals("1") ? View.VISIBLE : View.GONE);
            editText.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    item.setEditStr( s.toString());
                }

                @Override
                public void afterTextChanged(Editable s) {

                }
            });
        }

    }
}
